﻿using Excel2DbTool.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Excel2DbTool.Helpers
{
    public static class DataTableEx
    {
        public static IEnumerable<ExcelRowRecord> ToAGroupOfExcelRowRecord(this DataTable dt)
        {
            if (dt == null)
            {
                throw new ArgumentNullException("");
            };

            Int32 rowCount = dt.Rows.Count;
            List<ExcelRowRecord> excelRowRecords = new List<ExcelRowRecord>(rowCount);
            for (int i = 0; i < rowCount; ++i)
            {
                var err = new ExcelRowRecord();
                for (int j = 0; j < dt.Columns.Count; ++j)
                {
                    var cell = new Cell(dt.Columns[j].ColumnName, dt.Rows[i][j]);
                    err.AddCell(cell);
                }

                excelRowRecords.Add(err);
            }

            return excelRowRecords;
        }

    }
}
